package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {

    private String prefix="/business/bpmnInfo";
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;

    //去list页面
    @RequestMapping("/listPage")
    public String tolist(){
        return prefix+"/list";
    }
    //去文件部署页面
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix+"/deploy";
    }
    //去编辑页面
    @RequestMapping("/editPage")
    public String toedit(Long id, Model model){
        BpmnInfo bpmnInfo = bpmnInfoService.get(id);
        model.addAttribute("bpmnInfo",bpmnInfo);
        return prefix+"/edit";
    }

    //分页+过滤
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        TablePageInfo<BpmnInfo> bpmnInfos = bpmnInfoService.query(qo);
        return bpmnInfos;
    }
    //bpmn文件的上传
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String filePath=bpmnInfoService.upload(file);
        return new AjaxResult().success("文件上传成功！",filePath);
    }
    //添加自定义流程定义：是依赖activiti流程定义的部署的
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String filePath,String bpmnType, String deploymentName,String info ) throws FileNotFoundException {
        bpmnInfoService.deploy(filePath,bpmnType,deploymentName,info);
        return new AjaxResult().success("自定义流程定义信息部署成功！");
    }
    //编辑：自定义流程定义信息，只允许修改备注信息
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Long id,String info){
        bpmnInfoService.updateInfo(id,info);
        return new AjaxResult().success("自定义流程定义信息修改成功！");
    }
    //删除自定义的流程定义信息
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return new AjaxResult().success("自定义的流程定义信息删除成功！");
    }
    //查看流程文件：本质就是文件的下载(这时返回值就是void):是依赖activiti保存的bpmn文件的
    @RequestMapping("/readResource")
    public void readResource(Long deploymentId, String type,HttpServletResponse response) throws IOException {
        //根据部署的id,获取流程定义(里面有资源文件名)
        ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId.toString())
                .latestVersion()
                .singleResult();
        //源文件的输入流
        InputStream resourceAsStream=null;
        if("xml".equalsIgnoreCase(type)){
             resourceAsStream = repositoryService
                    .getResourceAsStream(deploymentId.toString(), processDefinition.getResourceName());
        }else if ("png".equalsIgnoreCase(type)){
            BpmnModel model = repositoryService.getBpmnModel(processDefinition.getId());
            ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            //generateDiagram(流程模型,需要高亮的节点,需要高亮的线条,后面三个参数都表示是字体)
            resourceAsStream = generator.generateDiagram(model, Collections.EMPTY_LIST, Collections.EMPTY_LIST,
                    "宋体","宋体","宋体");
        }

        //目标位置：浏览器
        IOUtils.copy(resourceAsStream,response.getOutputStream());

        resourceAsStream.close();
    }

}
